layui.use(['transfer', 'layer', 'util'], function(){
    var $ = layui.$
        ,transfer = layui.transfer
        ,layer = layui.layer
        ,util = layui.util;

        //查询一个人的·角色
        let updataRoleById= localStorage.getItem("updataRoleById");
        let name=localStorage.getItem("updataRoleByName");
            $("#updataRoleByName").text(name);
        let data=myAjaxGet("/user/userRoleFindById",{id:updataRoleById},"get");
    if (data.code==403){
        alert("没有相关权限");
        window.open("index.html","_self");
    }
        // console.log(data);
        data=data.data;
        let da;
        let data1='';
        for (let i=0;i<data.length;i++){
            da=data[i].code;
            data1 +='"'+data[i].id+'",'
        }
        data1= '['+data1+']';
        data1 = eval('(' + data1 + ')');
        // console.log(data1)

        //查询所有角色
        let dataAll=myAjax("/user/userRoleFindAll","","get");
    if (data.code==403){
        alert("没有相关权限");
        window.open("index.html","_self");
    }
        let d={
            id:updataRoleById
        }
        let dat=myAjax("/user/findById",d,"post");
        dataAll=dataAll.data;
        // console.log(data);
        let data2='';
        for (let i=0;i<dataAll.length;i++){
            data2 +='{"value": "'+dataAll[i].id+'", "title": "'+dataAll[i].typeName+'", "code": "'+dat.userbean.code+'"},'
        }
        data2= '['+data2+']';
        data2 = eval('(' + data2 + ')');
        // console.log(data2)

    //穿梭时的回调
    transfer.render({
        elem: '#test1'
        ,title: ['未拥有角色', '已拥有角色']
        ,data: data2
        ,value: data1
        ,onchange: function(obj, index){
            let arr = ['1', '2'];
            console.log(obj)
            if (arr[index]==1){
                let data=myAjax("/user/userRoleAdd",obj,"post");
                if (data.code==403){
                    alert("没有相关权限");
                    window.open("index.html","_self");
                }
                data=data.data
                if (data!=0){
                    layer.alert('成功添加角色'); //获得被穿梭时的数据
                }else {
                    layer.alert('添加角色失败');
                }
            }else if(arr[index]==2){
                let data=myAjax("/user/userRoleDelete",obj,"post");
                if (data.code==403){
                    alert("没有相关权限");
                    window.open("index.html","_self");
                }
                    data = data.data
                    if (data != 0) {
                        layer.alert('成功删除角色'); //获得被穿梭时的数据
                    } else {
                        layer.alert('删除角色失败');
                    }
            }
        }
    })
});